IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[PatientWard]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
   DROP TABLE [dbo].[PatientWard]
GO

/*----------------------------------------------------------------------
'Created By : Preeti
'Created On : 9/20/2008
'SP Name    : dbo.SP_SetPatientWard
'Used In    : 
'------------------------------------------------------------------------
'Modification Log  
'------------------------------------------------------------------------
'Created By        Modified By         Remarks                             
'-----------       ------------        ----------------------------------
'                                                                          
'----------------------------------------------------------------------*/

CREATE TABLE [dbo].[PatientWard] (
   ID   Varchar(30),
   [CRNumber]     Varchar(50),
   [wardname]     Varchar(50),
   [bedNumber]     Varchar(50),
   [startDate]     Datetime,
   [startTime]     Varchar(50),
   [endDate]     Datetime,
   [endTime]     Varchar(50)
)
GO

GRANT SELECT,INSERT,DELETE,UPDATE ON dbo.PatientWard TO hms_usr
GO

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id =
object_id(N'dbo.SP_GetPatientWard') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
   DROP PROCEDURE dbo.SP_GetPatientWard
GO


/*----------------------------------------------------------------------
'Created By : Preeti
'Created On : 9/20/2008
'SP Name    : dbo.SP_GetPatientWard
'Used In    : 
'------------------------------------------------------------------------
'Modification Log  
'------------------------------------------------------------------------
'Created By        Modified By         Remarks                             
'-----------       ------------        ----------------------------------
'                                                                          
'----------------------------------------------------------------------*/

CREATE PROCEDURE dbo.SP_GetPatientWard
@vchID VARCHAR(30)
AS
   SELECT
       ID,
       CRNumber,
       wardname,
       bedNumber,
       startDate,
       startTime,
       endDate,
       endTime
   FROM PatientWard
   WHERE ID=@vchID
GO

GRANT EXEC ON dbo.SP_GetPatientWard TO hms_usr
GO

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id =
object_id(N'dbo.SP_SetPatientWard') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
   DROP PROCEDURE dbo.SP_SetPatientWard
GO


/*----------------------------------------------------------------------
'Created By : Preeti
'Created On : 9/20/2008
'SP Name    : dbo.SP_SetPatientWard
'Used In    : 
'------------------------------------------------------------------------
'Modification Log  
'------------------------------------------------------------------------
'Created By        Modified By         Remarks                             
'-----------       ------------        ----------------------------------
'                                                                          
'----------------------------------------------------------------------*/

CREATE PROCEDURE dbo.SP_SetPatientWard
@vchID Varchar(30),
@vchCRNumber    Varchar(50),
@vchwardname    Varchar(50),
@vchbedNumber    Varchar(50),
@dtstartDate    Datetime,
@vchstartTime    Varchar(50),
@dtendDate    Datetime,
@vchendTime    Varchar(50)
AS
DECLARE  @iRetValue     integer
   IF NOT EXISTS(SELECT     ID
   FROM PatientWard
   WHERE   (ID = @vchID)
   )

   BEGIN
   INSERT INTO PatientWard(
       ID,
       CRNumber,
       wardname,
       bedNumber,
       startDate,
       startTime,
       endDate,
       endTime
  )
   VALUES (
      @vchID,
       @vchCRNumber,
       @vchwardname,
       @vchbedNumber,
       @dtstartDate,
       @vchstartTime,
       @dtendDate,
       @vchendTime
   )
   END
ELSE  IF EXISTS(SELECT     ID
   FROM PatientWard
   WHERE   (ID = @vchID)
   )

   BEGIN
   UPDATE PatientWard
   SET
       CRNumber = @vchCRNumber,
       wardname = @vchwardname,
       bedNumber = @vchbedNumber,
       startDate = @dtstartDate,
       startTime = @vchstartTime,
       endDate = @dtendDate,
       endTime = @vchendTime
  WHERE (ID = @vchID)
   END

IF (@@ERROR <> 0)
   BEGIN
       declare @SPErrMsg varchar(800)
       SET @SPErrMsg = 'Error: Error in Stored procedure dbo.SP_SetPatientWard for Loan# : ' + CAST (@vchID as varchar)
       RAISERROR (@SPErrMsg,16,1)
       Select @iRetValue = -1
   END
ELSE
   BEGIN
       Select @iRetValue = 0
   END
   
   RETURN @iRetValue
GO

GRANT EXEC ON dbo.SP_SetPatientWard TO hms_usr
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id =
object_id(N'dbo.SP_DeletePatientWard') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
   DROP PROCEDURE dbo.SP_DeletePatientWard
GO


/*----------------------------------------------------------------------
'Created By : Preeti
'Created On : 9/20/2008
'SP Name    : dbo.SP_DeletePatientWard
'Used In    : 
'------------------------------------------------------------------------
'Modification Log  
'------------------------------------------------------------------------
'Created By        Modified By         Remarks                             
'-----------       ------------        ----------------------------------
'                                                                          
'----------------------------------------------------------------------*/

CREATE PROCEDURE dbo.SP_DeletePatientWard
@vchID VARCHAR(30)
AS
   DELETE FROM PatientWard  WHERE   (ID = @vchID)

GRANT EXEC ON dbo.SP_DeletePatientWard TO hms_usr
GO

